class IncumbentEvaluation < ActiveRecord::Base
  belongs_to :incumbent
  
  def before_create
    self.med_times=0
    self.high_times=0
    self.low_times=0
  end
  
  def evaluate_red
    return Integer(self.confidence).to_s
  end
  
  def evaluate_green
    return Integer(10*normal_evaluation/sum_evaluation).to_s
  end
  
  def vote_incumbent(ip,vote)
    if ip==self.evaluated_ip
      return false
    else
      if vote.to_i==1
        self.high_times+=1
      elsif vote.to_i==2
        self.med_times+=1
      else
        self.low_times+=1
      end
      self.evaluated_ip=ip
      self.evaluated_at=Time.now
      self.save
      return true
    end
  end
  
private
  def sum_evaluation
    return self.med_times+self.high_times+self.low_times+1
  end
  
  def normal_evaluation
   return ([self.high_times,self.low_times].min+self.med_times).to_f
  end
end
